const { $Toast } = require('../../../utils/iview/base/index')
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';


let app = getApp()

Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
   
    sex: 1,
    items: [{
      name: '男',
      value: 1,
      checked: true
    },
    {
      name: '女',
      value: 2
    },
    ],
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    user: {},
  },



  onLoad(options) {
    var _that = this
    _that.setTitle('个人资料');
    
  

  },

  users() {
    var _that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      return;
    }
    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/getMember',
      data: { token: token },
      method: 'POST',
    }, '', res => {
      if (res.data.error_code == 0) {
       
        var user= res.data.data
       
        var items = _that.data.items;
        for (var i = 0; i < items.length; i++) {
          if (items[i].value == user.sex) {
            items[i].checked = true;
          } else {
            items[i].checked = false;
          }
        }
        _that.setData({
          user: user,
          sex: user.sex,
          items: items
        });
      }
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.users();
        }, 1500)
      }
    })
  },


  
  // order() {
  //   let _that = this;
  //   var token = wx.getStorageSync('token');
  //   if (!token) {
  //     return;
  //   }

  //   let net = new NetWork(_that.data._baseUrl)
  //   net._request({
  //     url: '/api/v1/checkMemberCode',
  //     data: {
  //       code: _that.data.code,
  //       phone :13250594982
  //     },
  //     method: 'POST'
  //   }, '', res => {

  //     _that.setData({
  //       order: res.data.data
  //     })
  //     console.log(res.data.data, "hhh")

  //   }, res => {
  //     if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
  //       app.refreshtoken(token);
  //       setTimeout(() => {
  //         _that.order();
  //       }, 1500)
  //     }
  //   });
  // },

  //   console.log(e.detail.value);
  //   var value = e.detail.value;
  //   this.setData({
  //     code: value
  //   })
  // },

  onShow: function () {
    var _that = this
    _that.setData({
      isclick: 0
    })
    _that.users();

  },
  radioChange(e) {
    var _that = this;
    _that.setData({
      sex: e.detail.value
    })

    _that.cation();
   
  },


  formSubmit(e) {
    var _that = this;

    var isclick = _that.data.isclick;

    if (isclick) {
      return;
    }

    _that.setData({
      isclick: 1
    })

    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 5000)


    var { value } = e.detail;
    _that.setData({
      dates: value
    })
    // if (!value) {
    //   value = _that.data.dates;
    // }
    value.token = wx.getStorageSync('token');

    
    let reg_tpl = /^1[3456789]\d{9}$/
    for (let i in value) {
      let values = value[i];
      if (!values) {
        $Toast({
          content: '内容不能为空',
          type: 'none'
        })
        return;
      }
      if (i == 'phone' && !reg_tpl.test(values)) {
        $Toast({
          content: '手机号码不正确!',
          type: 'none'
        })
        return;
      }

    }

    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/editMember',
      method: 'POST',
      data: value

    }, '修改', res => {
      setTimeout(() => {
        wx.navigateBack({
          changed: true
        });
      }, 1000)
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          $Toast({
            content: "修改失败，请重新修改",
            type: 'warning'
          })
        }, 1500)
      }
      else if (res.data.error_code == 400) {
       $Toast({
         content:"修改失败",
         type:'warning'
       })
      }

    })
  },

  modify(){
    wx.navigateTo({
      url: '../myInfo/modify',
    })
  },


  scanCode: function () {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          scanCodeMsg: res.result
        });
        wx.showToast({
          title: '成功',
          duration: 1000
        })
      }
    })
  },


  vehicleList() {
    wx.navigateTo({
      url: '../vehicleList/vehicleList',
    })
  },

 

  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },


  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })

        }
      }
    })
  },


  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    console.log(_userInfo)
    wx.setStorageSync('_userInfo', _userInfo);
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true,
      })
    }


    wx.login({
      success(res) {
        if (res.code) {


          net._request({
            url: 'api/v1/login/mini',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, '', res => {
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);

            _that.setData({
              userInfo: user
            })

            wx.setStorageSync('user', {
              user_type: 1
            });
            $Toast({
              content: res.data.msg,
              type: 'success'
            });

          }, res => {
            console.log("失败返回", res);
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);
            $Toast({
              content: res.data.msg,
              type: 'success'
            });
          });
        }
      }
    })
  },


})